using System;
using System.Xml;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Collections.Generic;


namespace MVCScaffolderXMLXML
{
    public class MVCGeneratorXML
    {
        public MVCGeneratorXML()
        {
            MVCGeneratorXMLEntityTypess = new List<MVCGeneratorXMLEntityTypes>();
        }

        // Attributes
        public String AddLinkToViewIndex { get; set; }

        public String AddScriptBundle { get; set; }

        public String AddStyleBundle { get; set; }

        public String AddScriptLayout { get; set; }

        public String AddStyleLayout { get; set; }

        public String InstallNuGet { get; set; }

        // Multi Instace objects
        public List<MVCGeneratorXMLEntityTypes> MVCGeneratorXMLEntityTypess { get; set; }

        public static MVCGeneratorXML CreateNewMVCGeneratorXML(System.Xml.XmlNode Node)
        {
            MVCGeneratorXML NewMVCGeneratorXML = new MVCGeneratorXML();

            foreach (XmlAttribute Attribute in Node.Attributes)
            {
                switch (Attribute.Name)
                {
                    case "AddLinkToViewIndex":
                        NewMVCGeneratorXML.AddLinkToViewIndex = Attribute.Value;
                         break;
                    case "AddScriptBundle":
                         NewMVCGeneratorXML.AddScriptBundle = Attribute.Value;
                         break;
                    case "AddStyleBundle":
                         NewMVCGeneratorXML.AddStyleBundle = Attribute.Value;
                         break;
                    case "AddScriptLayout":
                         NewMVCGeneratorXML.AddScriptLayout = Attribute.Value;
                         break;
                    case "AddStyleLayout":
                         NewMVCGeneratorXML.AddStyleLayout = Attribute.Value;
                         break;
                    case "InstallNuGet":
                         NewMVCGeneratorXML.InstallNuGet = Attribute.Value;
                         break;
                    default:
                        break;
                }
            }

            foreach (XmlNode ChildNode in Node.ChildNodes)
            {
                switch (ChildNode.Name)
                {
                    case "MVCGeneratorXMLEntityTypes":
                        MVCGeneratorXMLEntityTypes NewLocalMVCGeneratorXMLEntityTypes = MVCGeneratorXMLEntityTypes.CreateNewMVCGeneratorXMLEntityTypes(ChildNode);
                        NewMVCGeneratorXML.MVCGeneratorXMLEntityTypess.Add(NewLocalMVCGeneratorXMLEntityTypes);
                        break;

                    default:
                        break;
                }
            }

            return NewMVCGeneratorXML;
        }

        public void Save(String Filename)
        {
            XmlDocument Document = new XmlDocument();
            XmlNode docNode = Document.CreateXmlDeclaration("1.0", "UTF-8", null);
            Document.AppendChild(docNode);

            XmlNode MVCGeneratorXML = Document.CreateElement("MVCGeneratorXML");

            XmlAttribute AddLinkToViewIndex = Document.CreateAttribute("AddLinkToViewIndex");
            AddLinkToViewIndex.Value = this.AddLinkToViewIndex;
            MVCGeneratorXML.Attributes.Append(AddLinkToViewIndex);

            XmlAttribute AddScriptBundle = Document.CreateAttribute("AddScriptBundle");
            AddScriptBundle.Value = this.AddScriptBundle;
            MVCGeneratorXML.Attributes.Append(AddScriptBundle);

            XmlAttribute AddStyleBundle = Document.CreateAttribute("AddStyleBundle");
            AddStyleBundle.Value = this.AddStyleBundle;
            MVCGeneratorXML.Attributes.Append(AddStyleBundle);

            XmlAttribute AddScriptLayout = Document.CreateAttribute("AddScriptLayout");
            AddScriptLayout.Value = this.AddScriptLayout;
            MVCGeneratorXML.Attributes.Append(AddScriptLayout);

            XmlAttribute AddStyleLayout = Document.CreateAttribute("AddStyleLayout");
            AddStyleLayout.Value = this.AddStyleLayout;
            MVCGeneratorXML.Attributes.Append(AddStyleLayout);

            XmlAttribute InstallNuGet = Document.CreateAttribute("InstallNuGet");
            InstallNuGet.Value = this.InstallNuGet;
            MVCGeneratorXML.Attributes.Append(InstallNuGet);

            //MVCGeneratorXMLEntityTypes
              //<MVCGeneratorXMLEntityTypes Project="MVCApplication1" EntityContainer="CVEntities" EntityType="Educations">
            foreach (MVCGeneratorXMLEntityTypes item in MVCGeneratorXMLEntityTypess)
            {
                XmlNode MVCGeneratorXMLEntityTypes = Document.CreateElement("MVCGeneratorXMLEntityTypes");
                
                XmlAttribute Project = Document.CreateAttribute("Project");
                Project.Value = item.Project;
                MVCGeneratorXMLEntityTypes.Attributes.Append(Project);

                XmlAttribute EntityContainer = Document.CreateAttribute("EntityContainer");
                EntityContainer.Value = item.EntityContainer;
                MVCGeneratorXMLEntityTypes.Attributes.Append(EntityContainer);
                
                XmlAttribute EntityType = Document.CreateAttribute("EntityType");
                EntityType.Value = item.EntityType;
                MVCGeneratorXMLEntityTypes.Attributes.Append(EntityType);

                MVCGeneratorXML.AppendChild(MVCGeneratorXMLEntityTypes);

                foreach (Column CurrentColumn in item.Columns)
                {
                    XmlNode XMLColumn = Document.CreateElement("Column");
                    //<Column FieldName="EducationID" DataType="Int32" ShowInMasterView="True" ShowInDetailView="Edit" IsSearchable="True" ComboBoxColumn="Institution" Key="Key" DisplayField="DisplayField"></Column>
                    XmlAttribute FieldName = Document.CreateAttribute("FieldName");
                    FieldName.Value = CurrentColumn.FieldName;
                    XMLColumn.Attributes.Append(FieldName);

                    XmlAttribute DataType = Document.CreateAttribute("DataType");
                    DataType.Value = CurrentColumn.DataType;
                    XMLColumn.Attributes.Append(DataType);

                    XmlAttribute ShowInMasterView = Document.CreateAttribute("ShowInMasterView");
                    ShowInMasterView.Value = CurrentColumn.ShowInMasterView;
                    XMLColumn.Attributes.Append(ShowInMasterView);
                    
                    XmlAttribute ShowInDetailView = Document.CreateAttribute("ShowInDetailView");
                    ShowInDetailView.Value = CurrentColumn.ShowInDetailView;
                    XMLColumn.Attributes.Append(ShowInDetailView);

                    XmlAttribute IsSearchable = Document.CreateAttribute("IsSearchable");
                    IsSearchable.Value = CurrentColumn.IsSearchable;
                    XMLColumn.Attributes.Append(IsSearchable);

                    XmlAttribute ComboBoxColumn = Document.CreateAttribute("ComboBoxColumn");
                    IsSearchable.Value = CurrentColumn.IsSearchable;
                    XMLColumn.Attributes.Append(ComboBoxColumn);

                    XmlAttribute Key = Document.CreateAttribute("Key");
                    Key.Value = CurrentColumn.Key;
                    XMLColumn.Attributes.Append(Key);

                    XmlAttribute DisplayField = Document.CreateAttribute("DisplayField");
                    DisplayField.Value = CurrentColumn.DisplayField;
                    XMLColumn.Attributes.Append(DisplayField);
                    MVCGeneratorXMLEntityTypes.AppendChild(XMLColumn);                   

                }                
            }

            Document.AppendChild(MVCGeneratorXML);
            Document.Save(Filename + @"\MVCGenerator.XML");

        }
    }
}
